//
//  CZMeInfo.m
//  CZWeiXin
//
//  Created by Alvechen on 16/4/13.
//  Copyright © 2016年 alvechen. All rights reserved.
//

#import "CZMeInfoVC.h"

@interface CZMeInfoVC ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,XMPPvCardTempModuleDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *avatarImgV;

@property (weak, nonatomic) IBOutlet UILabel *nickNameLb;
@property (weak, nonatomic) IBOutlet UILabel *acountLb;

///我的资料卡
@property (nonatomic,strong) XMPPvCardTemp  *myvCardTemp;

@end

@implementation CZMeInfoVC

- (XMPPvCardTemp *)myvCardTemp{
    
    return [CZXMPPManager shareInstance].xmppvCardTempModule.myvCardTemp;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置个人资料模块代理
    [[CZXMPPManager shareInstance].xmppvCardTempModule addDelegate:self delegateQueue:dispatch_get_main_queue()];
    
    
    //更新UI
    [self updateUI];
}

/**
 * 更新UI
 */
- (void)updateUI{
    
    //头像
    self.avatarImgV.image = [UIImage imageWithData:self.myvCardTemp.photo];
    //昵称
    self.nickNameLb.text = self.myvCardTemp.nickname;
    //账号
    //    self.acountLb.text = self.myvCardTemp.jid.bare;//这个bare里没值
    self.acountLb.text = kCZ_myJID.bare;
}

/**
 * 头像点击
 */
- (IBAction)avatarClick:(id)sender {
    
    
    //弹出图片选取
    UIImagePickerController *picker = [UIImagePickerController new];
    //代理
    picker.delegate = self;
    //编辑
    picker.allowsEditing = YES;
    
    
    [self presentViewController:picker animated:YES completion:nil];

}

/**
 * 名称点击
 */
- (IBAction)nickNameClick:(id)sender {
    
    
    
}

#pragma mark - 图片选取完的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{


    NSLog(@"%@",info);
    
    //选中后编辑完的图片
    UIImage *img = info[UIImagePickerControllerEditedImage];
    
    //更新数据
    self.myvCardTemp.photo = UIImageJPEGRepresentation(img, 0.1);
    
    //更新我的个人资料
    [[CZXMPPManager shareInstance].xmppvCardTempModule updateMyvCardTemp:self.myvCardTemp];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - XMPPvCardTempModuleDelegate

/**
 * 更新我的资料卡成功
 */
- (void)xmppvCardTempModuleDidUpdateMyvCard:(XMPPvCardTempModule *)vCardTempModule{

    //更新UI
    [self updateUI];
}


@end
